<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// accounts : twitter, facebook, game center
// add account
// link account
// delete account
// get accounts

function add_twitter_account($user_id, $screen_name, $id_str, $name, $profile_image_url) {
    if(!$user_id || strlen($screen_name) <= 0 || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO twitter_accounts(user_id, screen_name, id_str, name, profile_image_url, timestamp)
                VALUES ('$user_id', '$screen_name', '$id_str', '$name', '$profile_image_url' , '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

function suggest_whiteboard_follows_by_twitter_ids($user_id, $twitter_ids) {
    if(!is_array($twitter_ids) || count($twitter_ids) <= 0) {
        return array();
    }
    $twitter_id_list = "('token'";
    foreach($twitter_ids as $twitter_id) {
        $twitter_id_list .= ", $twitter_id";
    }
    $twitter_id_list .= ")";
    $query = "SELECT user_id 
                FROM twitter_accounts 
                WHERE id_str IN $twitter_id_list
                AND user_id NOT IN (SELECT follow_id FROM whiteboard_follows WHERE user_id = '$user_id')";
    return safe_select_query($query);
}

function add_facebook_account($user_id, $username, $id_str, $email, $name, $first_name, $last_name, $profile_picture) {
    if(!$user_id || strlen($username) <= 0 || strlen($email) <= 0 || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO facebook_accounts(user_id, username, id_str, email, name, first_name, last_name, profile_picture, timestamp)
                VALUES ('$user_id', '$username', '$id_str', '$email', '$name', '$first_name', '$last_name', '$profile_picture' , '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

function suggest_whiteboard_follows_by_facebook_ids($user_id, $facebook_ids) {

    if(!is_array($facebook_ids) || count($facebook_ids) <= 0) {
        return array();
    }
    $facebook_id_list = "('token'";
    foreach($facebook_ids as $facebook_id) {
        $facebook_id_list .= ", $facebook_id";
    }
    $facebook_id_list .= ")";
    $query = "SELECT user_id 
                FROM facebook_accounts 
                WHERE id_str IN $facebook_id_list
                AND user_id NOT IN (SELECT follow_id FROM whiteboard_follows WHERE user_id = '$user_id')";

    return safe_select_query($query);
}

function suggest_whiteboard_xmpp_friend_by_facebook_ids($user_id, $facebook_ids) {

    if(!is_array($facebook_ids) || count($facebook_ids) <= 0) {
        return array();
    }
    $facebook_id_list = "('token'";
    foreach($facebook_ids as $facebook_id) {
        $facebook_id_list .= ", $facebook_id";
    }
    $facebook_id_list .= ")";
    $query = "SELECT user_id 
                FROM facebook_accounts 
                WHERE id_str IN $facebook_id_list
                AND user_id NOT IN (SELECT friend_id FROM whiteboard_friends WHERE user_id = '$user_id')
                AND user_id IN (SELECT DISTINCT(user_id) FROM whiteboard_xmpp_accounts)";
	//echo $query;
    return safe_select_query($query);
}

function add_gamecenter_account($user_id, $alias, $id_str) {
    if(!$user_id || strlen($alias) <= 0 || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO gamecenter_accounts(user_id, alias, id_str, timestamp)
                VALUES ('$user_id', '$alias', '$id_str', '$timestamp')";
    //echo $query;
    return safe_insert_query($query);
}

function suggest_whiteboard_follows_by_gamecenter_ids($user_id, $gamecenter_ids) {
    if(!is_array($gamecenter_ids) || count($gamecenter_ids) <= 0) {
        return array();
    }
    $gamecenter_id_list = "('token'";
    foreach($gamecenter_ids as $gamecenter_id) {
        $gamecenter_id_list .= ", $gamecenter_id";
    }
    $gamecenter_id_list .= ")";
    $query = "SELECT user_id 
                FROM gamecenter_accounts 
                WHERE id_str IN $gamecenter_id_list
                AND user_id NOT IN (SELECT follow_id FROM whiteboard_follows WHERE user_id = '$user_id')";
    return safe_select_query($query);
}

function link_twitter_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE twitter_accounts SET user_id = '$user_id', timestamp = '$timestamp'
                WHERE id_str = '$id_str'";
    return safe_update_query($query);
}

function link_facebook_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE facebook_accounts SET user_id = '$user_id', timestamp = '$timestamp'
                WHERE id_str = '$id_str'";
    return safe_update_query($query);
}

function link_gamecenter_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE gamecenter_accounts SET user_id = '$user_id', timestamp = '$timestamp'
                WHERE id_str = '$id_str'";
    return safe_update_query($query);
}

function unlink_twitter_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE twitter_accounts SET user_id = '0', timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND id_str = '$id_str'";
    return safe_update_query($query);
}

function unlink_facebook_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE facebook_accounts SET user_id = '0', timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND id_str = '$id_str'";
    return safe_update_query($query);
}

function unlink_gamecenter_account($user_id, $id_str) {
    if(!$user_id || !$id_str)
        return FALSE;
    $timestamp = time();
    $query = "UPDATE gamecenter_accounts SET user_id = '0', timestamp = '$timestamp'
                WHERE user_id = '$user_id' AND id_str = '$id_str'";
    return safe_update_query($query);
}

function get_twitter_accounts_of($user_id) {
    $query = "SELECT user_id, screen_name, id_str, name, profile_image_url, timestamp
                FROM twitter_accounts
                WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

function get_facebook_accounts_of($user_id) {
    $query = "SELECT user_id, username, id_str, email, name, first_name, last_name, profile_picture, timestamp
                FROM facebook_accounts
                WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

function get_gamecenter_accounts_of($user_id) {
    $query = "SELECT user_id, alias, id_str, timestamp
                FROM gamecenter_accounts
                WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

function get_user_id_by_twitter($id_str) {
    $query = "SELECT user_id
                FROM twitter_accounts
                WHERE id_str = '$id_str'";
//    echo $query;
    $result = safe_select_query($query);
//    echo '-'.$result[0]->user_id.'-';
    return $result[0]->user_id;
}

function get_user_id_by_facebook($id_str) {
    $query = "SELECT user_id
                FROM facebook_accounts
                WHERE id_str = '$id_str'";
//    echo $query;
    $result = safe_select_query($query);
//    echo '-'.$result[0]->user_id.'-';
    return $result[0]->user_id;
}

function get_user_id_by_gamecenter($id_str) {
    $query = "SELECT user_id
                FROM gamecenter_accounts
                WHERE id_str = '$id_str'";
    $result = safe_select_query($query);
    return $result[0];
}

function get_user_ids_by_twitters($id_strs) {
    $id_str_array = "(-1";
    foreach($id_strs as $id_str) {
        $id_str_array .= ", '$id_str'";
    }
    $id_str_array .= ")";
    $query = "SELECT user_id
                FROM twitter_accounts
                WHERE id_str IN $id_str_array";
    $result = safe_select_query($query);
    return $result;
}

function get_user_ids_by_facebooks($id_strs) {
    $id_str_array = "(-1";
    foreach($id_strs as $id_str) {
        $id_str_array .= ", '$id_str'";
    }
    $id_str_array .= ")";
    $query = "SELECT user_id
                FROM facebook_accounts
                WHERE id_str IN $id_str_array";
    $result = safe_select_query($query);
    return $result;
}

function get_user_ids_by_gamecenters($id_strs) {
    $id_str_array = "(-1";
    foreach($id_strs as $id_str) {
        $id_str_array .= ", '$id_str'";
    }
    $id_str_array .= ")";
    $query = "SELECT user_id
                FROM gamecenter_accounts
                WHERE id_str IN $id_str_array";
    $result = safe_select_query($query);
    return $result;
}

?>
